home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gpt32src.zip / BIVARIAT.DEM < prev    next >
Text File  |  1992-03-25  |  3KB  |  116 lines

  1. #
  2. # $Id: bivariat.demo,v 3.26 92/03/24 22:32:38 woo Exp Locker: woo $
  3. #
  4. #
  5. # This demo is very slow and requires unusually large stack size.
  6. # Do not attempt to run this demo under MSDOS.
  7. #
  8.  
  9. # the function integral_f(x) approximates the integral of f(x) from 0 to x.
  10. # integral2_f(x,y) approximates the integral from x to y.
  11. # define f(x) to be any single variable function
  12. #
  13. # the integral is calculated as the sum of f(x_n)*delta 
  14. #   do this x/delta times (from x down to 0)
  15. #
  16. f(x) = exp(-x**2)
  17. delta = 0.025
  18. #
  19. # integral_f(x) takes one variable, the upper limit.  0 is the lower limit.
  20. # calculate the integral of function f(t) from 0 to x
  21. integral_f(x) = (x>0)?integral1a(x):-integral1b(x)
  22. integral1a(x) = (x<=0)?0:(integral1a(x-delta)+delta*f(x))
  23. integral1b(x) = (x>=0)?0:(integral1b(x+delta)+delta*f(x))
  24. #
  25. # integral2_f(x,y) takes two variables; x is the lower limit, and y the upper.
  26. # claculate the integral of function f(t) from x to y
  27. integral2_f(x,y) = (x<y)?integral2(x,y):-integral2(y,x)
  28. integral2(x,y) = (x>y)?0:(integral2(x+delta,y)+delta*f(x))
  29.  
  30. set title "approximate the integral of functions"
  31. set samples 50
  32.  
  33. plot [-5:5] f(x) title "f(x)=exp(-x**2)", 2/sqrt(pi)*integral_f(x) title "erf(x)=2/sqrt(pi)*integral_f(x)"
  34.  
  35. pause -1 "Hit return to continue"
  36.  
  37. f(x)=sin(x)
  38.  
  39. plot [-5:5] f(x) title "f(x)=sin(x)", integral_f(x)
  40.  
  41. pause -1 "Hit return to continue"
  42.  
  43. set title "approximate the integral of functions (upper and lower limits)"
  44.  
  45. f(x)=(x-2)**2-20
  46.  
  47. plot [-10:10] f(x) title "f(x)=(x-2)**2-20", integral2_f(-5,x)
  48.  
  49. pause -1 "Hit return to continue"
  50.  
  51. f(x)=sin(x-1)-.75*sin(2*x-1)+(x**2)/8-5
  52.  
  53. plot  [-10:10] f(x) title "f(x)=sin(x-1)-0.75*sin(2*x-1)+(x**2)/8-5", integral2_f(x,1)
  54.  
  55. pause -1 "Hit return to continue"
  56.  
  57. #
  58. # This definition computes the ackermann. Do not attempt to compute its
  59. # values for non integral values. In addition, do not attempt to compute
  60. # its beyond m = 3, unless you want to wait really long time.
  61.  
  62. ack(m,n) = (m == 0) ? n + 1 : (n == 0) ? ack(m-1,1) : ack(m-1,ack(m,n-1))
  63.  
  64. set xrange [0:3]
  65. set yrange [0:3]
  66.  
  67. set isosamples 4
  68. set samples 4
  69.  
  70. set title "Plot of the ackermann function"
  71.  
  72. splot ack(x, y)
  73.  
  74. pause -1 "Hit return to continue"
  75.  
  76. set xrange [-5:5]
  77. set yrange [-10:10]
  78. set isosamples 10
  79. set samples 100
  80. set key 4,-3
  81. set title "Min(x,y) and Max(x,y)"
  82.  
  83. #
  84. min(x,y) = (x < y) ? x : y
  85. max(x,y) = (x > y) ? x : y
  86.  
  87. plot sin(x), x**2, x**3, max(sin(x), min(x**2, x**3))+0.5
  88.  
  89. pause -1 "Hit return to continue"
  90.  
  91. #
  92. # gcd(x,y) finds the greatest common divisor of x and y,
  93. #          using Euclid's algorithm
  94. # as this is defined only for integers, first round to the nearest integer
  95. gcd(x,y) = gcd1(rnd(max(x,y)),rnd(min(x,y)))
  96. gcd1(x,y) = (y == 0) ? x : gcd1(y, x - x/y * y)
  97. rnd(x) = int(x+0.5)
  98.  
  99. set samples 59
  100. set xrange [1:59]
  101. set auto
  102. set key
  103.  
  104. set title "Greatest Common Divisor (for integers only)"
  105.  
  106. plot gcd(x, 60)
  107. pause -1 "Hit return to continue"
  108.  
  109. set xrange [-10:10]
  110. set yrange [-10:10]
  111. set auto
  112. set isosamples 10
  113. set samples 100
  114. set title ""
  115.  
  116.